<?php

namespace Trawind\Helpers;


use GuzzleHttp\Client;
use Illuminate\Support\Str;
use Psr\Http\Message\ResponseInterface;
use Trawind\Basics\Traits\GuzzleHttpClient;
use Trawind\Basics\Traits\HasFactory;

/**
 * Class HttpClientHelper
 *
 * @package Trawind\Helpers
 */
class HttpClientHelper
{

    use GuzzleHttpClient, HasFactory;


    public static function guzzle(array $config = ['verify' => false]): Client
    {
        return new Client($config);
    }


    public static function isRemoteRequest(): bool
    {
        return request()->input('source') === 'remote';
    }


    /**
     * @param  ResponseInterface  $response
     * @return array
     */
    public static function getResponseContent(ResponseInterface $response): array
    {
        return json_decode($response->getBody()->getContents(), true);
    }


    public static function requestLog(array $data)
    {
        if ($requestId = request()->header('x-request-id')) {
            $key = 'log:request:'.date('m/d/H:').$requestId;

            $data['app'] = config('app.name');
            $data['time'] = date('Y-m-d H:i:s');
            $redis = SystemHelper::redis();
            $redis->rPush($key, json_encode($data));
            $redis->expire($key, 60 * 20); //20分钟过期
        }
    }


    public static function getRemoteParam()
    {
        $params['remote'] = explode(',', request('remote'));
        foreach ($params['remote'] as $remote) {
            foreach (explode(',', request('remoteInclude')) as $value) {
                if (Str::contains($value, $remote)) {
                    $include = ($params['include'][$remote] ?? '').','.str_replace("{$remote}.", '', $value);
                    $params['include'][$remote] = ltrim($include, ',');
                }
            }
            foreach (explode(';', request('remoteWith')) as $value) {
                if (Str::contains($value, $remote)) {
                    $with = ($params['with'][$remote] ?? '').';'.str_replace("{$remote}.", '', $value);
                    $params['with'][$remote] = ltrim($with, ';');
                }
            }
            foreach (explode(';', request('remoteSearch')) as $value) {
                if (Str::contains($value, $remote)) {
                    $search = ($params['search'][$remote] ?? '').';'.str_replace("{$remote}.", '', $value);
                    $params['search'][$remote] = ltrim($search, ';');
                }
            }
            foreach (explode(';', request('remoteSearchFields')) as $value) {
                if (Str::contains($value, $remote)) {
                    $searchFields = ($params['searchFields'][$remote] ?? '').';'.str_replace("{$remote}.", '', $value);
                    $params['searchFields'][$remote] = ltrim($searchFields, ';');
                }
            }
            foreach (explode(';', request('remoteOrderBy')) as $value) {
                if (Str::contains($value, $remote)) {
                    $orderBy = ($params['orderBy'][$remote] ?? '').';'.str_replace("{$remote}.", '', $value);
                    $params['orderBy'][$remote] = ltrim($orderBy, ';');
                }
            }
            foreach (explode(';', request('remoteSortedBy')) as $value) {
                if (Str::contains($value, $remote)) {
                    $sortedBy = ($params['sortedBy'][$remote] ?? '').';'.str_replace("{$remote}.", '', $value);
                    $params['sortedBy'][$remote] = ltrim($sortedBy, ';');
                }
            }
        }

        return $params;
    }

}
